package com.cswangdao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cswangdao.annotation.OperationLog;
import com.cswangdao.domain.dto.AdminInfoDTO;
import com.cswangdao.domain.dto.AdminPageDTO;
import com.cswangdao.domain.model.MarketAdmin;
import com.cswangdao.domain.po.Admin;
import com.cswangdao.domain.vo.AdminInfo;
import com.cswangdao.domain.vo.AdminPageVo;
import com.cswangdao.domain.vo.AdminVo;
import com.cswangdao.mapper.MarketAdminMapper;
import com.cswangdao.result.PageResult;
import com.cswangdao.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.function.Function;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    MarketAdminMapper mapper;

    /**
     * @description: 登录校验接口
     * @param: map ，session
     * @return: com.cswangdao.domain.vo.AdminVo
     */

    //@OperationLog(value = "用户登录")
    @Override
    public AdminVo login(Map<String, String> map, HttpSession session) {

             String username = map.get("username");
             Admin admin = mapper.login(username);
             if (Objects.isNull(admin) || !map.get("password").equals(admin.getPassword()))
                 return null;
             AdminVo adminVo = new AdminVo();
             AdminInfoDTO adminInfoDTO = new AdminInfoDTO();
             adminInfoDTO.setAvatar(admin.getAvatar());
             adminInfoDTO.setNickName(admin.getUsername());
             adminVo.setAdminInfo(adminInfoDTO);
             session.setAttribute("adminId", admin.getId());
             session.setAttribute("username", username);
             adminVo.setToken(session.getId());
             return adminVo;

     }


    /**
     * @description: 返回登录信息
     * @param: adminId
     * @return: com.cswangdao.domain.vo.AdminInfo
     */
    @Override
    public AdminInfo getAdminInfoById(Integer adminId) {
        if (Objects.isNull(adminId)){
            throw new RuntimeException("adminId不能为空");
        }
        try {
            Admin admin = mapper.getAdminInfoById(adminId);
            List<String> perms = new ArrayList<>();
            perms.add("*");
            List<String> roles = new ArrayList<>();
            roles.add("超级管理员");
            AdminInfo adminInfo = AdminInfo.builder()
                    .name(admin.getUsername())
                    .perms(perms)
                    .roles(roles)
                    .avatar(admin.getAvatar())
                    .build();

            return adminInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public PageResult<AdminPageVo> pageQueryInfo(AdminPageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(),pageDTO.getLimit());
        Page<MarketAdmin> page = mapper.pageQueryInfo(pageDTO);
        List<MarketAdmin> marketAdmins = page.getResult();
        List<AdminPageVo> adminPageVos = BeanUtil.copyToList(marketAdmins, AdminPageVo.class);
        return PageResult.builder()
                .pages(page.getPages())
                .total(page.getTotal())
                .list((List) adminPageVos)
                .page(pageDTO.getPage())
                .limit(pageDTO.getLimit())
                .build();
    }

    @Override
    public void addAdmin(MarketAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        mapper.addAdmin(admin);
    }

    @Override
    public void updateById(MarketAdmin admin) {
        mapper.updateById(admin);
    }

    @Override
    public void deleteById(Integer id) {
        mapper.deleteById(id);
    }

}
